<?
require("config.php");

$location_status .= " | Edit";

$show_backnav = "category_menu";

$menuname = NULL;

$menutarget = NULL;

if (NULL!=$action){

	$dir = "$table/";

/*	if ($Name!=$bk_Name) {
		$Query = "select id from $table where name = '$Name'";
		$r = myQuery($Query);
		if ($r[0][0])	{
			header("location:" . $rela_path . "share/msg.php?v=9000");
			exit();
		}
	}
*/
	if ($Parent) {
		$Query = "select level from $table where id = '$Parent'";
		$r = myQuery($Query);
		$level = $r[0][0]+1;
	}
	else $level=0;

	$product_str='|';
	for ($i=0;$i<count($product);$i++) $product_str.=$product[$i].'|';
	

	if ($Logo) myUpload($Logo,$Logo_name,$dir);

	if ($Logo_name == null) $Logo_name = $bk_Logo;

	if ($remove_Logo=="ture") $Logo_name=NULL;


   $field = array(
	"code", 
	"name",
	"cname", 
	"logo", 
	"parent",
	"product",
	"level",
	"sort",
	"publish",
	"limits"
	);


   $content = array(
	$Code,
	$Name,
	$CName,
	$Logo_name,
	$Parent,
	$product_str,
	$level,
	$Sort,
	$publish,
	$limit
	);

   $expression = "id = $id";

   myUpdate($table,$field,$content,$expression);

   $field = array(
	"parent",
	);


   $content = array(
	$Name,
	);
	
   myUpdate($table,$field,$content,"parent='$bk_Name'");
	

   $field = array(
	"category",
	"parent_category",
	);

   $content = array(
	$Name,
	$Parent,
	);

   $expression = "category = '$bk_Name'";

   myUpdate("pro_basic",$field,$content,$expression);

   header("location:" . $rela_path . "share/msg.php?v=11");

}else{
	$Query = "select id, code, name,cname, logo, parent ,product,sort,publish,limits from $table where id = $id";
	$r = myQuery($Query);

	if (!$r)header("location:" . $rela_path . "share/msg.php?v=9002");
	$Code = $r[0][1];
	$Name = $r[0][2];
	$CName = $r[0][3];
	$Logo = $r[0][4];
	$Parent = $r[0][5];
	$product_str=$r[0][6];
	$Sort=$r[0][7];
	$publish=$r[0][8];
	$limit=$r[0][9];
	
	$Query="select id,code,name from pro_basic where publish='1' order by id desc;";
	$r=myQuery($Query);
	$select='';
	for ($i=0 ; $i<count($r) ; $i++) {
		if (substr_count($product_str,'|'.$r[$i][0].'|')) $s=' selected'; else $s='';
		$select.='<option value="'.$r[$i][0].'"'.$s.'>'.$r[$i][1].'-'.$r[$i][2].'</option>';
	}


}

require($rela_path . "inc/header.php");

?>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
</script>

<form action="" method="post" enctype="multipart/form-data" name="form1">
<table width="95%" border="0" align="center">
  <tr> 
    <td align="right">&nbsp; </td>
  </tr>
  <tr> 
    <td><p><br>
        </p>
        <table>
          <tr> 
            <td class="tdheader">Code :</td>
            <td> <input type="text" class="text" name="Code" size="40" value=<?print($Code)?>> 
              <br> 
              <!--hidden field-->
              <input name="action" type="hidden" value="edit"> 
			  <input name="id" type="hidden" value="<?print($id)?>"> 
			  <input name="bk_Logo" type="hidden" value="<?print($Logo)?>"> 
			  <input name="bk_Name" type="hidden" value="<?print($Name)?>"> 
			  <input name="bk_Parent" type="hidden" value="<?print($Parent)?>">
            </td>
          </tr>

<?


$boxName="Parent";

$tb = "pro_category";

$Query = "select name,id,cname from $tb WHERE level=0 order by sort,id";
$r = myQuery($Query);

$Query = "select name, parent,id,cname from $tb WHERE level=1 order by sort,id";
$r1 = myQuery($Query);

$sourceData = array();
$sourceLabel = array();

	array_push($sourceData,'0');
	array_push($sourceLabel,'[No Parent]');

for ($i=0;$i<count($r);$i++){

	array_push($sourceData,$r[$i][1]);
	array_push($sourceLabel,$r[$i][0].' '.$r[$i][2]);

	for ($n=0;$n<count($r1);$n++){

		if ($r[$i][1]==$r1[$n][1]){

			array_push($sourceData,$r1[$n][2]);
			array_push($sourceLabel,"-&nbsp;&nbsp;&nbsp;" . $r1[$n][0].' '.$r1[$n][3] );

		}
	}

}


?>



          <tr> 
            <td class="tdheader">Parent:</td>
            <td><?FFPSelectBox("Parent",$Parent,$sourceData,$sourceLabel)?></td>
          </tr>
          <tr> 
            <td class="tdheader">Name :</td>
            <td><input type="text" class="text" name="Name" size="40" value="<?print($Name)?>"></td>
          </tr>
          <tr> 
            <td class="tdheader">Chinese Name :</td>
            <td><input type="text" class="text" name="CName" size="40" value="<?print($CName)?>"></td>
          </tr>
          <tr> 
            <td class="tdheader">Sort :</td>
            <td><input type="text" class="text" name="Sort" size="40" value="<?=$Sort?>"></td>
          </tr>
          <tr> 
            <td class="tdheader">Publish :</td>
            <td>              <select name="publish" id="publish">
              <option value="1" <? if ($publish) echo 'selected';?>>Yes</option>
              <option value="0" <? if (!$publish) echo 'selected';?>>No</option>
            </select></td>
          </tr>
          <tr> 
            <td class="tdheader">Sales Limit  :</td>
            <td><input name="limit" type="text" class="text" id="limit" size="40" value="<?=$limit?>"></td>
          </tr>
        <!--  <tr> 
            <td class="tdheader">Logo :</td>
            <td><?loadImage($Logo,$rela_path . $PIC_DIR . "$table/")?>
			<?removeImage($Logo,"remove_Logo")?>
			<input name="Logo" type="File" class="text" size="40"></td>
          </tr>
		  <tr> 
            <td class="tdheader">Related Items:<br>
            (Allow Mutiple)</td>
            <td><select name="product[]" size="5" multiple>
			<?=$select?>
            </select>              </td>
          </tr>-->
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="image" src="../../images/bt_cancel.gif" onClick="Redirect('category_menu.php',1);return false;"> <input type="image" onClick="MM_validateForm('Code','','R','Name','','R','CName','','R','Sort','','RisNum','limit','','NisNum');return document.MM_returnValue" src="../../images/bt_save.gif">
            </td>
          </tr>
        </table>
        <p><br>
          <br>
          <br>
          <br>
          <br>
        </p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp; </p></td>
  </tr>
</table>
</form>
<?
require($rela_path . "inc/footer.php");
?>
